package guesswords.khova.com;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.SurfaceGestureDetector;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;

import android.view.KeyEvent;
import android.view.MotionEvent;

//===================================STRUCTS=================================
class Blocks{
	 boolean active; // Once clicked blocks go from active to inactive
}

/*
 * Handles uiPosition Tracking
 */
class uiPosition{
	
	float x;
	float y;
	float x1;
	float y1;
	
	public uiPosition ()
	{
		x = 0;
		y = 0;
		x1 = 0;
		y1 = 0;
	}
	
	public void register(float posX, float posY)
	{
		// Store current touch position
		x1 = x;
		y1 = y;
		x = posX;
		y = posY;
	}
}


/*
 * UIScrollBlock handles the HUD display for choosing letters
 */
public class UIScrollBlock{
	
	// Constants
	final int MAXLETTERS = 25; //Letters in Alphabet (0 - 25)
	final char XAXIS = 'x';
	final char YAXIS ='y';
	
	// Vars
	TextureRegion[] mScrollBlock;
	Texture mTexture;
	
	uiPosition touchPosition = new uiPosition();

	
	int scrollBlockWidth;
	int scrollBlockHeight;
	int hudLetterLength; // Number of Letters to display in UI
	int focusLetter; // Letter Position on HUD that has focus
	public int chosenLetter; // Letter chosen via the HUD
	
	int uiPosX; // Set position of UI on screen
	int uiPosY; 
	
	float lastTouchX; // Last Touch Position X
	float lastTouchY; // Last Touch Position Y
	
	boolean needUIRefresh; // Flag for Refreshing the UI

	Blocks[] block = new Blocks[MAXLETTERS]; 
	
	/* 
	 * Initialize the UIScrollBlock Class
	 */
	public UIScrollBlock(){
		
		// Initializing the Blocks
		for (int x = 0; x < MAXLETTERS; x++){	
			block[x] = new Blocks();
			block[x].active = true;	
		}	
		
		scrollBlockWidth = 25; // Default Block Width
		scrollBlockHeight = 25;// Default Block Height
		
		uiPosX = 10; uiPosY = 10; // Default Position 
		hudLetterLength = 5; // Default Hud Length
		focusLetter = 0; // Default Letter A
		
		needUIRefresh = false; // Flag for refreshing the UI
		
		
	
	}
	
	/*
	 * Load and sorts the Texture for the letters
	 */
	public void setTexture(Texture fTexture, TextureRegion[] fTextureRegion){
		
		mTexture =  fTexture;
		mScrollBlock = fTextureRegion;
		loadScrollBlock(); // Breaks up the texture regions to correspond with letters
	}
		
	/*
	 * Change Focus Letter to set value
	 */
	public void setFocusLetter(int value)
	{
		focusLetter = value;
	}
	
	/*
	 * Move Focus Letter Up One
	 */
	public void focusLetterUp()
	{
		focusLetter += 1;
		
		// OverFlow Check
		if (focusLetter > 25) { focusLetter = 0; }
		
	}
	/*
	 * Move Focus Letter Down One
	 */
	public void focusLetterDown()
	{
		focusLetter -= 1;
	
		// Overflow Check
		if (focusLetter < 0) { focusLetter = 25; }
		
	}
	
	
	/*
	 * Change Block Width and Height Default settings
	 */
	public void setBlockDim(int width, int height){
	
		scrollBlockWidth = width;
		scrollBlockHeight = height;
		
	}
	
	/*
	 * Change UI Position on the screen Default settings
	 */
	public void setUIPosition(int posX, int posY){
		
		uiPosX = posX;
		uiPosY = posY;
		
	}
	
	public boolean checkRefresh()
	{
		boolean check;
		
		check = needUIRefresh;
		
		needUIRefresh = false;
		
		return check;
	}
	
	
	/*
	 * Loads the UI display into the scene for rendering
	 */
	public Scene loadDisplay(Scene scene){
		
		Sprite[] letterSprite = new Sprite[hudLetterLength];
		int offset = (hudLetterLength - 1)/ 2;
		int letterValue = 0;
		int posX;

		
		for (int x = 0; x < hudLetterLength; x++){	
			
			letterValue = (focusLetter - offset) + x;
			
			// If LetteValue is negative put it back on track
			if (letterValue < 0) { letterValue = letterValue + (MAXLETTERS + 1); }
			if (letterValue > 25){ letterValue = letterValue - (MAXLETTERS + 1); }
			
			posX = uiPosX + (scrollBlockWidth * x);
			
			letterSprite[x] = new Sprite(posX, uiPosY , mScrollBlock[letterValue])
		      {
	                @Override
	                public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
	                                float pTouchAreaLocalX, float pTouchAreaLocalY)
	                {
	                	   MLog.i(GuessWordz.TAG, "Touch Me" );
	                	   registerChoice();
	                	   
	                	   
	                	  
	                	touchPosition.register(pTouchAreaLocalX, pTouchAreaLocalY); 
	              
	                	                
	                	   if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_MOVE)
	                       {
	
	                		   if (swipedRight()) {  
		                			   MLog.i(GuessWordz.TAG, "swipe right" );
		                			   focusLetterUp();     			   
	                			   }
	                		   if (swipedLeft()) 
	                		   {
	                			   MLog.i(GuessWordz.TAG, "swipe left" );
	                			   focusLetterDown();
	                		   }
	                           
	                       }
	                	   
	                	   if(pSceneTouchEvent.getAction() == KeyEvent.KEYCODE_DPAD_LEFT)
	                	   {
	            			   MLog.i(GuessWordz.TAG, "swipe left" );
                			   focusLetterDown();
	                	
	                	   }
	                	   
	                	   if(pSceneTouchEvent.getAction() == KeyEvent.KEYCODE_DPAD_RIGHT)
	                	   {
	            			   MLog.i(GuessWordz.TAG, "swipe right" );
                			   focusLetterUp();
	                	
	                	   }
	                	
	                	
	                	   
	                       return true;
	                }
	        };
			
	        scene.registerTouchArea(letterSprite[x]);
			scene.getLastChild().attachChild(letterSprite[x]);
			
		}
	
		MLog.i(GuessWordz.TAG, "UI Display Started!");
		
		return scene;
		
	}
	
	//************************ INTERNAL FUNCTIONS
	
	
	/*
	 * Assign the Texture Location from the letter map to the Letter variable
	 */
	private void loadScrollBlock()
	{
		
		mScrollBlock[0] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'a', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'a',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[1] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'b', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'b',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[2] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'c', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'c',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[3] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'d', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'d',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[4] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'e', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'e',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[5] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'f', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'f',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[6] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'g', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'g',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[7] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'h', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'h',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[8] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'i', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'i',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[9] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'j', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'j',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[10] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'k', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'k',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[11] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'l', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'l',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[12] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'m', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'m',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[13] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'n', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'n',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[14] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'o', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'o',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[15] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'p', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'p',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[16] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'q', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'q',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[17] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'r', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'r',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[18] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'s', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'s',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[19] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'t', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'t',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[20] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'u', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'u',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[21] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'v', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'v',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[22] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'w', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'w',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[23] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'x', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'x',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[24] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'y', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'y',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
		mScrollBlock[25] = TextureRegionFactory.extractFromTexture(mTexture, findLetterGFX(XAXIS,'z', scrollBlockWidth, scrollBlockHeight), findLetterGFX(YAXIS,'z',scrollBlockWidth, scrollBlockHeight), scrollBlockWidth, scrollBlockHeight);
	}
	
	/*
	 * Check for Right Swipe
	 */
	private boolean swipedRight()
	{
		boolean value;
		float sum;
		
		value = true; // Default value

		
		sum = touchPosition.y1 - touchPosition.y;
		MLog.i(GuessWordz.TAG, "TouchPos Values Y|Y1");
		MLog.i(GuessWordz.TAG, String.valueOf(touchPosition.y ));
		MLog.i(GuessWordz.TAG, String.valueOf(touchPosition.y1 ));
		MLog.i(GuessWordz.TAG, String.valueOf(sum));
		
		if (touchPosition.y1 <= touchPosition.y) {value = false;}
		
		needUIRefresh = true;
		
		return value;
	}
	
	private boolean swipedLeft()
	{
		boolean value;
		
		value = true; // Default value
		
		if (touchPosition.y1 >= touchPosition.y) {value = false;}
		
		needUIRefresh = true;
		
		return value;
	}
	
	private void registerChoice()
	{
		chosenLetter = focusLetter;
	}
	
	/*
	 * Coordinate Letter/Value with Position
	 * TODO Need to move this into a public function // used also in GameBoardView
	 */
	private int findLetterGFX(char axis, char theletter, int width, int height)
    {
        int xpos, ypos, value;
 
        switch (theletter)
        {
            case 'a':
                xpos = 0; ypos = 0;
                break;
            case 'b':
                xpos = width; ypos = 0;
                break;
            case 'c':
                xpos = width * 2; ypos = 0;
                break;
            case 'd':
                xpos = width * 3; ypos = 0;
                break;
            case 'e':
                xpos = width * 4; ypos = 0;
                break;
            case 'f':
                xpos = 0; ypos = height;
                break;
            case 'g':
                xpos = width; ypos = height;
                break;
            case 'h':
                xpos = width * 2; ypos = height;
                break;
            case 'i':
                xpos = width * 3; ypos = height;
                break;
            case 'j':
                xpos = width * 4; ypos = height;
                break;
            case 'k':
                xpos = 0; ypos = height * 2;
                break;
            case 'l':
                xpos = width; ypos = height * 2;
                break;
            case 'm':
                xpos = width * 2; ypos = height * 2;
                break;
            case 'n':
                xpos = width * 3; ypos = height * 2;
                break;
            case 'o':
                xpos = width * 4; ypos = height * 2;
                break;
            case 'p':
                xpos = 0; ypos = height * 3;
                break;
            case 'q':
                xpos = width; ypos = height * 3;
                break;
            case 'r':
                xpos = width * 2; ypos = height * 3;
                break;
            case 's':
                xpos = width * 3; ypos = height * 3;
                break;
            case 't':
                xpos = width * 4; ypos = height * 3;
                break;
            case 'u':
                xpos = 0; ypos = height * 4;
                break;
            case 'v':
                xpos = width; ypos = height * 4;
                break;
            case 'w':
                xpos = width * 2; ypos = height * 4;
                break;
            case 'x':
                xpos = width * 3; ypos = height * 4;
                break;
            case 'y':
                xpos = width * 4; ypos = height * 4;
                break;
            case 'z':
                xpos = 0; ypos = height * 5;
                break;
            case '-':
                xpos = width * 1; ypos = height * 5;
                break;
            case '/':
                xpos = width * 2; ypos = height * 5;
                break;
            case '\'':
                xpos = width * 3; ypos = height * 5;
                break;
            case ',':
                xpos = width * 4; ypos = height * 5;
                break;
                /*
            case REDBLOCK:
            	xpos = width * 1; ypos = height * 6;
            	break;
            case BLUEBLOCK:
            	xpos = width * 0; ypos = height * 6;
            	break;
                 */

            default:
                xpos = 0; ypos = 0;

                break;

        }
        if (axis == 'x')
        {
            value = xpos;
        }
        else
        {
            value = ypos;
        }
        return value;


    }
	

}

